package pers.dawnyang.config.aspect;

import java.util.List;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.log4j.Log4j2;
import pers.dawnyang.config.cache.DictCacheService;;

/**
 * 统一处理service请求返回参数
 * 
 * @author dawn yang
 * @date 2021/05/06
 */

@Aspect
@Order(3)
@Component
@Log4j2
public class ServiceParamAspect {

  @Autowired
  private DictCacheService dictCacheService;

  // @Pointcut("execution(*
  // pers.dawnyang.modular.*.service.impl..*.*(..))")
  @Pointcut("@annotation(pers.dawnyang.common.core.annotation.SP)")
  public void servicePointcut() {}

  @SuppressWarnings({"unchecked", "rawtypes"})
  @Around("servicePointcut()")
  public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
    /*
     * Object[] args = pjp.getArgs(); for
     * (Object arg : args) {
     * //log.info("请求值是 " + arg); }
     */
    Object res = pjp.proceed();
    if (res instanceof IPage) {
      List<?> records = ((IPage<?>) res).getRecords();
      dictCacheService.transList(records);
    } else if (res instanceof List) {
      dictCacheService.transList((List) res);
    } else {
      dictCacheService.trans(res);
    }
    // Gson gson = new Gson();
    // log.info("返回值是 " +
    // gson.toJson(result));
    return res;
  }
}
